/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * GUICadastroDeSocio.java
 *
 * Created on 22/04/2011, 12:14:14
 */

package net.antoniopassos.dukeclube.visao.gui;

import java.awt.Dimension;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.text.ParseException;
import net.antoniopassos.dukeclube.excecao.DukeClubeException;
import net.antoniopassos.dukeclube.modelo.dominio.Socio;
import net.antoniopassos.dukeclube.modelo.dominio.constante.CategoriaDeSocio;
import net.antoniopassos.dukeclube.modelo.dominio.constante.Constante;
import net.antoniopassos.dukeclube.modelo.dominio.constante.Sexo;

/**
 *
 * @author Marlon
 */
public class GUICadastroDeSocio extends javax.swing.JInternalFrame {

    /** Creates new form GUICadastroDeSocio */
    public GUICadastroDeSocio() {
        initComponents();
        for (CategoriaDeSocio categoria : CategoriaDeSocio.values()) {
          cbCategoria.addItem(categoria.name());
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        bgSexo = new javax.swing.ButtonGroup();
        pDadosSocio = new javax.swing.JPanel();
        iCategoria = new javax.swing.JLabel();
        cbCategoria = new javax.swing.JComboBox();
        iNome = new javax.swing.JLabel();
        tfNome = new javax.swing.JTextField();
        iSexo = new javax.swing.JLabel();
        rbFemenino = new javax.swing.JRadioButton();
        rbMasculino = new javax.swing.JRadioButton();
        iDataNascimento = new javax.swing.JLabel();
        tfDataNascimento = new javax.swing.JFormattedTextField();
        iTelefoneFixo = new javax.swing.JLabel();
        tfTelefoneFixo = new javax.swing.JFormattedTextField();
        iTelefoneCelular = new javax.swing.JLabel();
        tfTelefoneCelular = new javax.swing.JFormattedTextField();
        iEmail = new javax.swing.JLabel();
        tfEmail = new javax.swing.JTextField();
        bGravarSocio = new javax.swing.JButton();
        bLimpar = new javax.swing.JButton();
        bCancelar = new javax.swing.JButton();

        setClosable(true);

        pDadosSocio.setBorder(javax.swing.BorderFactory.createTitledBorder("Dados do Socio"));

        iCategoria.setText("Categoria");

        cbCategoria.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        iNome.setText("Nome");

        iSexo.setText("Sexo");

        bgSexo.add(rbFemenino);
        rbFemenino.setText("Feminino");

        bgSexo.add(rbMasculino);
        rbMasculino.setText("Masculino");

        iDataNascimento.setText("Data de nascimento");

        try {
            tfDataNascimento.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##/##/####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }

        iTelefoneFixo.setText("Telefone Fixo");

        try {
            tfTelefoneFixo.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("(##) ####-####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }

        iTelefoneCelular.setText("Telefone Celular");

        try {
            tfTelefoneCelular.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("(##) ####-####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }

        iEmail.setText("Email");

        bGravarSocio.setText("Gravar");

        bLimpar.setText("Limpar");
        bLimpar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bLimparActionPerformed(evt);
            }
        });

        bCancelar.setText("Cancelar");
        bCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bCancelarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pDadosSocioLayout = new javax.swing.GroupLayout(pDadosSocio);
        pDadosSocio.setLayout(pDadosSocioLayout);
        pDadosSocioLayout.setHorizontalGroup(
            pDadosSocioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pDadosSocioLayout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addGroup(pDadosSocioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(iSexo)
                    .addComponent(iNome)
                    .addComponent(iCategoria)
                    .addComponent(iDataNascimento)
                    .addComponent(iTelefoneFixo)
                    .addComponent(iEmail))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pDadosSocioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(tfEmail, javax.swing.GroupLayout.PREFERRED_SIZE, 194, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(pDadosSocioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(cbCategoria, javax.swing.GroupLayout.PREFERRED_SIZE, 142, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(rbFemenino, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(tfNome, javax.swing.GroupLayout.PREFERRED_SIZE, 194, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(pDadosSocioLayout.createSequentialGroup()
                            .addGroup(pDadosSocioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(tfDataNascimento, javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(rbMasculino, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 85, Short.MAX_VALUE)
                                .addComponent(tfTelefoneFixo, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(iTelefoneCelular)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(tfTelefoneCelular, javax.swing.GroupLayout.DEFAULT_SIZE, 116, Short.MAX_VALUE))))
                .addGap(91, 91, 91))
            .addGroup(pDadosSocioLayout.createSequentialGroup()
                .addComponent(bGravarSocio)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(bLimpar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(bCancelar)
                .addContainerGap(289, Short.MAX_VALUE))
        );
        pDadosSocioLayout.setVerticalGroup(
            pDadosSocioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pDadosSocioLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pDadosSocioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cbCategoria, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(iCategoria))
                .addGap(12, 12, 12)
                .addGroup(pDadosSocioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tfNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(iNome))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(pDadosSocioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(rbFemenino)
                    .addComponent(iSexo))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(rbMasculino)
                .addGap(6, 6, 6)
                .addGroup(pDadosSocioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tfDataNascimento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(iDataNascimento))
                .addGap(6, 6, 6)
                .addGroup(pDadosSocioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tfTelefoneFixo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(iTelefoneFixo)
                    .addComponent(tfTelefoneCelular, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(iTelefoneCelular))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pDadosSocioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tfEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(iEmail))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 53, Short.MAX_VALUE)
                .addGroup(pDadosSocioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(bGravarSocio, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(bLimpar, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(bCancelar, javax.swing.GroupLayout.Alignment.TRAILING)))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(pDadosSocio, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(pDadosSocio, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void bCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bCancelarActionPerformed
      dispose();        // TODO add your handling code here:
    }//GEN-LAST:event_bCancelarActionPerformed

    private void bLimparActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bLimparActionPerformed
      limparCampos();  // TODO add your handling code here:
    }//GEN-LAST:event_bLimparActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bCancelar;
    private javax.swing.JButton bGravarSocio;
    private javax.swing.JButton bLimpar;
    private javax.swing.ButtonGroup bgSexo;
    private javax.swing.JComboBox cbCategoria;
    private javax.swing.JLabel iCategoria;
    private javax.swing.JLabel iDataNascimento;
    private javax.swing.JLabel iEmail;
    private javax.swing.JLabel iNome;
    private javax.swing.JLabel iSexo;
    private javax.swing.JLabel iTelefoneCelular;
    private javax.swing.JLabel iTelefoneFixo;
    private javax.swing.JPanel pDadosSocio;
    private javax.swing.JRadioButton rbFemenino;
    private javax.swing.JRadioButton rbMasculino;
    private javax.swing.JFormattedTextField tfDataNascimento;
    private javax.swing.JTextField tfEmail;
    private javax.swing.JTextField tfNome;
    private javax.swing.JFormattedTextField tfTelefoneCelular;
    private javax.swing.JFormattedTextField tfTelefoneFixo;
    // End of variables declaration//GEN-END:variables

public void setPosicao() {
   Dimension d = this.getDesktopPane().getSize();
   this.setLocation(15 + (d.width - this.getSize().width) / 5, 15 + (d.height -
   this.getSize().height) / 5);
}

private Socio socio;

public Socio getSocio() throws DukeClubeException {
   int categoria = cbCategoria.getSelectedIndex();
   if (categoria == 0) {
       throw new DukeClubeException("Não foi informada a categoria do sócio.");
   } else {
        socio.setCategoria(categoria - 1);
     }
   String nome = tfNome.getText();
   if (nome == null || nome.trim().equals("")){
      tfNome.requestFocus();
      throw new DukeClubeException("Não foi informado o nome do sócio.");
   } else {
        socio.setNome(nome);
   }

   if (rbFemenino.isSelected()) {
        socio.setSexo(Sexo.Femenino.ordinal());
   } else if (rbMasculino.isSelected()){
        socio.setSexo(Sexo.Masculino.ordinal());
   } else {
        throw new DukeClubeException("Não foi informado o sexo do sócio.");
   }

   DateFormat df = DateFormat.getDateInstance();
   df.setLenient(false);
   String strDataNascimento = tfDataNascimento.getText();
   try {
        java.util.Date dataNascimento = df.parse(strDataNascimento);
        socio.setDataNascimento(dataNascimento);
   } catch (ParseException ex) {
        tfDataNascimento.requestFocus();
        String mensagem = "Não informada a data de nascimento ou\ninformada uma data inválida.";
        throw new DukeClubeException(mensagem);
    } catch (DukeClubeException ex) {
        tfDataNascimento.requestFocus();
        throw new DukeClubeException(ex.getMessage());
      }
    socio.setTelefoneFixo(tfTelefoneFixo.getText());
    socio.setTelefoneCelular(tfTelefoneCelular.getText());
    socio.setEmail(tfEmail.getText());
    return socio;
}

public void exibeDadosDoSocio() {
   limparCampos();
   if (socio.getCodigo() != Constante.NOVO) {
      cbCategoria.setSelectedIndex(socio.getCategoria() + 1);
      tfNome.setText(socio.getNome());
      if (socio.getSexo() == Sexo.Femenino.ordinal()) {
         rbFemenino.setSelected(true);
      }
      if (socio.getSexo() == Sexo.Masculino.ordinal()) {
        rbMasculino.setSelected(true);
      }

      DateFormat df = DateFormat.getDateInstance();
      java.util.Date dataNascimento = socio.getDataNascimento();
      if (dataNascimento != null) {
        tfDataNascimento.setText(df.format(dataNascimento));
      }
      tfTelefoneFixo.setText(socio.getTelefoneFixo());
      tfTelefoneCelular.setText(socio.getTelefoneCelular());
      tfEmail.setText(socio.getEmail());
   }
}

public void setSocio(Socio socio){
  this.socio = socio;
  this.exibeDadosDoSocio();
}

public void bGravarSocioAddActionListener(ActionListener ouvinte) {
  bGravarSocio.addActionListener(ouvinte);
}


public void limparCampos(){
  cbCategoria.setSelectedIndex(0);
  tfNome.setText(null);
  tfDataNascimento.setText(null);
  tfTelefoneFixo.setText(null);
  tfTelefoneCelular.setText(null);
  tfEmail.setText(null);
}

}
